package cn.stylefeng.guns.modular.ctm.service.impl;

import cn.hutool.core.lang.UUID;
import cn.stylefeng.guns.modular.ctm.dto.ResumeInformationDTO;
import cn.stylefeng.guns.modular.ctm.entity.*;
import cn.stylefeng.guns.modular.ctm.mapper.ResumeInformationMapper;
import cn.stylefeng.guns.modular.ctm.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * <p>
 * 简历信息
 * </p>
 *
 * @author yzb
 * @since 2022-05-24
 */
@Service
public class ResumeInformationServiceImpl extends ServiceImpl<ResumeInformationMapper, ResumeInformation> implements ResumeInformationService {

    @Autowired
    EducationService educationService;
    @Autowired
    WorkService workService;
    @Autowired
    TrainService trainService;
    @Autowired
    CertificateService certificateService;
    @Autowired
    LanguageService languageService;
    @Autowired
    WorksService worksService;

    @Override
    public ResumeInformationDTO getInfo() {
        ResumeInformationDTO resumeInformationDTO = new ResumeInformationDTO();
        //todo 获取在线用户信息
        String userId = "77d6d66479cb80fd1b70736a36a65912";
        ResumeInformation resumeInformation = baseMapper.getInfo(userId);
        if (ObjectUtils.isNotEmpty(resumeInformation)) {
            resumeInformationDTO.setResumeInformation(resumeInformation);
            String id = resumeInformation.getId();

            List<Education> educationList = educationService.getBaseMapper().selectList(new LambdaQueryWrapper<Education>().eq(Education::getResumeInformationId, id));
            resumeInformationDTO.setEducationList(educationList);

            List<Work> workList = workService.getBaseMapper().selectList(new LambdaQueryWrapper<Work>().eq(Work::getResumeInformationId, id));
            resumeInformationDTO.setWorkList(workList);

            List<Train> trainList = trainService.getBaseMapper().selectList(new LambdaQueryWrapper<Train>().eq(Train::getResumeInformationId, id));
            resumeInformationDTO.setTrainList(trainList);

            List<Certificate> certificateList = certificateService.getBaseMapper().selectList(new LambdaQueryWrapper<Certificate>().eq(Certificate::getResumeInformationId, id));
            resumeInformationDTO.setCertificateList(certificateList);

            List<Language> languageList = languageService.getBaseMapper().selectList(new LambdaQueryWrapper<Language>().eq(Language::getResumeInformationId, id));
            resumeInformationDTO.setLanguageList(languageList);

            List<Works> worksList = worksService.getBaseMapper().selectList(new LambdaQueryWrapper<Works>().eq(Works::getResumeInformationId, id));
            resumeInformationDTO.setWorksList(worksList);
        }
        return resumeInformationDTO;
    }

    @Override
    @Transactional
    public boolean save(ResumeInformationDTO resumeInformationDTO) {
        boolean save;
        String resumeInformationId = UUID.randomUUID().toString();
        ResumeInformation resumeInformation = resumeInformationDTO.getResumeInformation();
        if (StringUtils.isBlank(resumeInformation.getId())) {
            resumeInformation.setId(resumeInformationId);
            save = super.save(resumeInformation);
        } else {
            save = super.updateById(resumeInformation);
            resumeInformationId = resumeInformation.getId();
        }
        saveTheRelatedTableInformation(resumeInformationDTO, resumeInformationId);
        return save;
    }

    /**
     * 保存关联表信息
     *
     * @param resumeInformationDTO
     * @param numericUUID
     */
    private void saveTheRelatedTableInformation(ResumeInformationDTO resumeInformationDTO, String numericUUID) {
        List<Education> educationList = resumeInformationDTO.getEducationList();
        educationList.stream().forEach(e -> e.setResumeInformationId(numericUUID));
        educationService.removeByResumeInformationId(numericUUID);
        educationService.saveBatch(educationList);

        List<Certificate> certificateList = resumeInformationDTO.getCertificateList();
        certificateList.stream().forEach(e -> e.setResumeInformationId(numericUUID));
        certificateService.removeByResumeInformationId(numericUUID);
        certificateService.saveBatch(certificateList);

        List<Language> languageList = resumeInformationDTO.getLanguageList();
        languageList.stream().forEach(e -> e.setResumeInformationId(numericUUID));
        languageService.removeByResumeInformationId(numericUUID);
        languageService.saveBatch(languageList);

        List<Train> trainList = resumeInformationDTO.getTrainList();
        trainList.stream().forEach(e -> e.setResumeInformationId(numericUUID));
        trainService.removeByResumeInformationId(numericUUID);
        trainService.saveBatch(trainList);

        List<Work> workList = resumeInformationDTO.getWorkList();
        workList.stream().forEach(e -> e.setResumeInformationId(numericUUID));
        workService.removeByResumeInformationId(numericUUID);
        workService.saveBatch(workList);

        List<Works> worksList = resumeInformationDTO.getWorksList();
        worksList.stream().forEach(e -> e.setResumeInformationId(numericUUID));
        worksService.removeByResumeInformationId(numericUUID);
        worksService.saveBatch(worksList);
    }
}
